//
//  CHDBaseTableView.m
//  IOSWeiiPhoneNews
//
//  Created by ibokan on 14-4-23.
//  Copyright (c) 2014年 chen. All rights reserved.
//

#import "CHDBaseTableView.h"
#import "MJRefresh.h"
@interface CHDBaseTableView ()
{
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
}
@end
@implementation CHDBaseTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 3.1.下拉刷新 当下拉条显示的时候
        if (self.hidePullDown == NO) {
            [self addHeader];
        }
        
        // 3.2.上拉加载更多
        if (self.hidePullUp == NO) {
            [self addFooter];
        }
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)addHeader
{
    //    __unsafe_unretained RootViewController *vc = self;
    
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // 进入刷新状态就会回调这个Block
        //下拉事件
        if ([self.pullDelegate respondsToSelector:@selector(pullDown:)]) {
            [self.pullDelegate pullDown:self];
        }
        
        // 这里的refreshView其实就是header
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:2.0];
        
    };
    
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
    };
    
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                //                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
                
            case MJRefreshStatePulling:
                //                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
                
            case MJRefreshStateRefreshing:
                //                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
    //    [header beginRefreshing];
    _header = header;
}

- (void)addFooter
{
    //    __unsafe_unretained RootViewController *vc = self;
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        //上拉事件
        if ([self.pullDelegate respondsToSelector:@selector(pullUp:)]) {
            [self.pullDelegate pullUp:self];
        }
        
        // 这里的refreshView其实就是footer
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:2.0];
        
    };
    _footer = footer;
}



- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 刷新表格
    //    [self reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}

//结束刷新
-(void)pullEnd{
    [_header endRefreshing];
}

@end
